<template>
  <!-- treeDefaultExpandAll -->
  <a-tree-select
    :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
    :treeData="TreeData"
    :placeholder="placeholder"
    v-model="curValue"
    @change="handleSelected"
    v-bind="$attrs"
    :load-data="onLoadData"
    style="width: 100%;"
    :getPopupContainer="triggerNode => triggerNode.parentNode"
    allowClear
  ></a-tree-select>
</template>
<script>
export default {
  props: {
    placeholder: { type: String, required: false, default: '类型' },
    value: { type: String, required: false }
  },
  data() {
    return {
      curValue: '',
      data: [],
      TreeData: [
        // {
        //   Code: '11',
        //   id: '1484526495538876496',
        //   Level: 1,
        //   ParentId: null,
        //   Remark: null,
        //   title: '铝模材料',
        //   value: '1484526495538876496' },
        // {
        //   Code: '12',
        //   id: '1484526495538876497',
        //   Level: 1,
        //   ParentId: null,
        //   Remark: null,
        //   title: '铝模材料456',
        //   value: '1484526495538876497' }
      ]
    }
  },
  watch: {
    value(value) {
      //console.log(value)
      this.curValue = value
    }
  },
  created() {
    this.curValue = this.value
    this.getEnumItem()
  },
  methods: {
    // onLoadData(treeNode) {
    //   //console.log(treeNode)
    //   //console.log(treeNode.dataRef)
    // },
    onLoadData(treeNode) {
      //console.log(treeNode)
      //console.log(treeNode.dataRef)
      const thisObj = this
      const { Id } = treeNode.dataRef
      return new Promise((resolve, reject) => {
        thisObj.$http.post('/ProductManagement/nventory/GetDataList' + Id, {}).then(resJson => {
          var data = resJson.Data
          // data.forEach(item => {
          //   // item.pId = item.ParentId
          //   // item.id = item.Id
          //   this.$set(item, 'isLeaf', true)
          // })
          // treeNode.dataRef.children = data
          this.$set(treeNode.dataRef, 'children', data)
          // //console.log(this.TreeData)
          // this.TreeData = this.TreeData.concat(data)
          //console.log(treeNode.dataRef)
          resolve()
        })
      })
    },
    genTreeNode(parentId, isLeaf = false) {
      const random = Math.random()
        .toString(36)
        .substring(2, 6)
      return {
        id: random,
        pId: parentId,
        value: random,
        title: isLeaf ? 'Tree Node' : 'Expand to load',
        isLeaf
      }
    },
    async getEnumItem(id = '') {
      await this.$http.post('/Clt/Clt_Product/GetDataList' + id).then(resJson => {
        this.TreeData = resJson.Data
        // this.data.forEach(item => {
        //   // item.children = null
        //   this.TreeData.push(item)
        // })
      })
    },
    handleSelected(val) {
      //console.log(val)
      this.$emit('input', val)
    }
  }
}
</script>
